package com.px.user.controller;


import com.px.common.core.Response;
import com.px.user.entity.RedPack;
import com.px.user.entity.RedPackGrab;
import com.px.user.mapper.RedPackMapper;
import com.px.user.model.PagingResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.sql.Timestamp;
import java.util.Collection;

@Slf4j
@Validated
@RestController
@RequestMapping("/redPack")
@Api(value = "红包管理", tags = {"红包管理"})
public class RedPackController {

    @Autowired
    private RedPackMapper redPackMapper;

    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation(value = "发红包记录搜索", authorizations = {@Authorization(value = "token")}, tags = {"红包"})
    public Response<PagingResp<RedPack>> redPackGet(
            @ApiParam(value = "红包id(管理员使用)") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "创建人id(管理员使用)") @RequestParam(value = "creatorId", required = false) Long creatorId,
            @ApiParam(value = "房间id(管理员使用)") @RequestParam(value = "roomId", required = false) Long roomId,
            @ApiParam(value = "红包分类：one#定向红包,random#随机红包(管理员使用)") @RequestParam(value = "category", required = false) String category,
            @ApiParam(value = "开始时间戳", example = "1566805792293") @RequestParam(value = "start", required = false) Long qstart,
            @ApiParam(value = "结束时间戳", example = "1566805792293") @RequestParam(value = "end", required = false) Long qend,
            @ApiParam(value = "排序字段(倒序)", example = "updateTime") @RequestParam(value = "orderField", required = false, defaultValue = "createTime") String orderField,
            @ApiParam(value = "页码", example = "0") @RequestParam(value = "page", required = false, defaultValue = "0") @Min(0) Integer page,
            @ApiParam(value = "返回数量", example = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") @Min(1) Integer limit,
            Authentication authentication) {
        RedPack qredPack = RedPack.builder()
                .id(id)
                .creatorId(creatorId)
                .roomId(roomId)
                .category(category)
                .qstart(qstart == null ? null : new Timestamp(qstart))
                .qend(qend == null ? null : new Timestamp(qend))
                .orderField(orderField)
                .startIndex(page * limit)
                .limit(limit)
                .build();

        Collection roles = authentication.getAuthorities();
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("ADMIN");
        if (!roles.contains(admin))
            qredPack.setCreatorId(Long.valueOf(authentication.getName()));

        return Response.success(PagingResp.<RedPack>builder()
                .itemCount(redPackMapper.pagingCount(qredPack))
                .items(redPackMapper.paging(qredPack))
                .build());
    }

    @GetMapping("/grabSearch")
    @ApiOperation(value = "抢红包搜索", authorizations = {@Authorization(value = "token")}, tags = {"红包"})
    public Response<PagingResp<RedPackGrab>> redPackGrabGet(
            @ApiParam(value = "抢红包id(管理员使用)") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "红包ID(管理员使用)") @RequestParam(value = "redPackId", required = false) Long redPackId,
            @ApiParam(value = "抢包人ID(管理员使用)") @RequestParam(value = "toUserId", required = false) Long toUserId,
            @ApiParam(value = "开始时间戳", example = "1566805792293") @RequestParam(value = "start", required = false) Long qstart,
            @ApiParam(value = "结束时间戳") @RequestParam(value = "end", required = false) Long qend,
            @ApiParam(value = "排序字段(倒序)", example = "updateTime") @RequestParam(value = "orderField", required = false, defaultValue = "createTime") String orderField,
            @ApiParam(value = "页码", example = "0") @RequestParam(value = "page", required = false, defaultValue = "0") @Min(0) Integer page,
            @ApiParam(value = "返回数量", example = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") @Min(1) Integer limit,
            Authentication authentication) {
        RedPackGrab qredPackGrab = RedPackGrab.builder()
                .id(id)
                .redPackId(redPackId)
                .toUserId(toUserId)
                .qstart(qstart == null ? null : new Timestamp(qstart))
                .qend(qend == null ? null : new Timestamp(qend))
                .orderField(orderField)
                .startIndex(page * limit)
                .limit(limit)
                .build();

        Collection roles = authentication.getAuthorities();
        SimpleGrantedAuthority admin = new SimpleGrantedAuthority("ADMIN");
        if (!roles.contains(admin))
            qredPackGrab.setToUserId(Long.valueOf(authentication.getName()));
        return Response.success(PagingResp.<RedPackGrab>builder()
                .itemCount(redPackMapper.grabPagingCount(qredPackGrab))
                .items(redPackMapper.grabPaging(qredPackGrab))
                .build());
    }
}
